package com.xusheng.cli.controller.open;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xusheng.cli.entity.paymentChannelModule.PaymentChannel;
import com.xusheng.cli.entity.rechargeModule.Recharge;
import com.xusheng.cli.service.PaymentChannelService;
import com.xusheng.cli.service.RechargeService;
import com.xusheng.cli.service.WithdrawService;
import com.xusheng.cli.service.impl.FakaPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.TreeMap;

@RestController
@RequestMapping("/open/v1/fakapay")
@Slf4j
public class FakapayNotifyController extends BaseNotifyController {

    @Resource
    private PaymentChannelService paymentChannelService;
    @Resource
    private RechargeService rechargeService;
    @Resource
    private WithdrawService withdrawService;

    @RequestMapping("/notify")
    public Object notify(HttpServletRequest request, HttpServletResponse response, @RequestBody TreeMap<String, String> reqParam) {
        try {
            log.info("发卡交易异步通知 参数:{}", reqParam);
            String orderNo = reqParam.get("requestId");
            //获取订单信息
            LambdaQueryWrapper<Recharge> qw = new LambdaQueryWrapper<>(new Recharge()).eq(Recharge::getOrderNumber, orderNo).last("limit 1");
            Recharge order = rechargeService.getOne(qw);
            PaymentChannel paymentChannel = paymentChannelService.getById(order.getPaymentId());
            String status = reqParam.get("status");
            String signVer = FakaPayServiceImpl.getSign(reqParam, paymentChannel.getSecret());
            if (StrUtil.isEmpty(orderNo)) {
                log.info("发卡交易推送过来到商户订单号数值为空，不进行后续操作");
                return "fail";
            }

            if (!"SUCCESS".equals(status)) {
                log.info("发卡交易推送状态不为SUCCESS,订单号:{},status:{}", orderNo, status);
                return "SUCCESS";
            }
            if (reqParam.get("code").equalsIgnoreCase(signVer.trim())) {
                super.notifyAsync(request, response, orderNo);
            } else {
                log.info("发卡交易推送验签失败");
            }
        } catch (Exception e) {
            log.error("发卡交易异步通知业务逻辑处理失败，具体原因:{}", e, e.getMessage());
        }
        return "SUCCESS";

    }

    @Override
    protected void doNotifyBusiness(HttpServletRequest request, HttpServletResponse response, String orderNo) {

//        PrintWriter pw = null;
//        String resCode = "SUCCESS";
        try {
//            pw = response.getWriter();
            if (StrUtil.isEmpty(orderNo)) {
                log.info("发卡交易异步通知 订单号数值为空，不进行后续操作");
                return;
            }
            //订单后续处理
            rechargeService.confirmPay(orderNo);

        } catch (Exception e) {
            log.error("发卡交易异步通知 验签业务逻辑处理失败，具体原因:{}", e, e.getMessage());
        }
    }

    /**
     * 代付回调
     *
     * @param reqParam
     * @return
     */
    @RequestMapping("/paynotify")
    public Object payNotify(@RequestBody TreeMap<String, String> reqParam) {

        return withdrawService.payNotify(reqParam);

    }
}
